********************************************************************************
* MSCI DATA
use "$datapath/MSCI_exceldata_to_DTA.dta", clear
run "$buildpath/0_countryselection" // 36 countries
keep if datem >= ym(2002,1) & datem <= ym(2016,7)

* normalize msci price index
bys country (datem) : gen usdmsci_norm = 100 if _n == 1
bys country (datem) : replace usdmsci_norm = 100*usdmsci/usdmsci[1] if _n > 1

* earnings and dividend and books
qui gen l_E = ln(ep*usdmsci_norm)
qui gen l_D = ln(dy*usdmsci_norm)
qui gen l_B = ln(mvalue / usdmsci_norm)

bys country (datem) : gen dl_D = l_D - l_D[_n-1]
bys country (datem) : gen dl_E = l_E - l_E[_n-1]
bys country (datem) : gen dl_B = l_B - l_B[_n-1]

tempfile msci
save `msci', replace
********************************************************************************

********************************************************************************
* INDUSTRIAL PRODUCTION
use "$datapath/industrialproduction.dta", clear
keep if datem >= ym(2002,1) & datem <= ym(2016,7)

* log transformation
gen l_Y = ln(Y)
* first difference
bys country (datem) : gen dl_Y = l_Y - l_Y[_n-1]

tempfile Y
save `Y', replace
********************************************************************************

********************************************************************************
* BOND DATA
use "$datapath/bonds.dta", clear
keep if datem >= ym(2002,1) & datem <= ym(2016,7)

* log transformation
gen l_H = ln(bondindex)
* first difference
bys country (datem) : gen dl_H = l_H - l_H[_n-1]

tempfile bond
save `bond', replace
********************************************************************************

********************************************************************************
use "$datapath/Portfolio_Paper_zbar.dta", clear

merge m:1 datem country  using `msci' , nogenerate
merge m:1 datem country  using `Y' , nogenerate
merge m:1 datem country  using `bond' , nogenerate
merge m:1 datem country  using "$datapath/Interest_Rates_exceldata_to_DTA.dta" , keepusing(R3m dR3m) nogenerate

keep if datem >= ym(2002,1) & datem <= ym(2016,7)
drop if mi(fundid)
********************************************************************************

********************************************************************************
* BENCHMARK INSTRUMENTS
keep datem fundid country zbar* l_E l_D l_B dl_D dl_E dl_B l_Y R3m dR3m dl_Y l_H dl_H
********************************************************************************

*******************************************************************************
tempfile master
save `master', replace

*******************************************************************************
*** fund weighted us differential
levelsof country, local(country)
foreach c of local country {
	*** main
	qui gen s_E_`c' = zbar_`c'*l_E
	qui gen s_D_`c' = zbar_`c'*l_D
	qui gen s_B_`c' = zbar_`c'*l_B
	
	qui gen s_dE_`c' = zbar_`c'*dl_E
	qui gen s_dD_`c' = zbar_`c'*dl_D
	qui gen s_dB_`c' = zbar_`c'*dl_B
			
	qui gen s_Y_`c' = zbar_`c'*l_Y
	qui gen s_dY_`c' = zbar_`c'*dl_Y
	
	qui gen s_H_`c' = zbar_`c'*l_H
	qui gen s_dH_`c' = zbar_`c'*dl_H
	
	qui gen s_I_`c' = zbar_`c'*R3m
	qui gen s_dI_`c' = zbar_`c'*dR3m
}
*******************************************************************************

*******************************************************************************
* reduce information and reshape the data
qui collapse (sum) s_* , by(fundid datem)

foreach v of varlist s_* {
	qui replace `v' = . if `v' == 0
}

global singlesum "s_E_ s_dE_ s_D_ s_dD_ s_Y_ s_I_ s_dI_ s_dY_ s_B_ s_dB_ s_H_ s_dH_  "

qui reshape long $singlesum , i(datem fundid) j(country) string 
drop if mi(s_Y_)
*******************************************************************************

*******************************************************************************
* import us differentials
merge 1:1 fundid country datem using `master', nogenerate
***************************************************************************

* main
gen z_E = l_E - s_E_
gen z_dE = dl_E - s_dE_

gen z_D = l_D - s_D_
gen z_dD = dl_D - s_dD_

gen z_B = l_B - s_B_
gen z_dB = dl_B - s_dB_

gen z_Y = l_Y - s_Y_
gen z_dY = dl_Y - s_dY_

gen z_H = l_H - s_H_
gen z_dH = dl_H - s_dH_

gen z_I = R3m - s_I_
gen z_dI = dR3m - s_dI_

keep datem fundid country z_* 

sort fundid country datem

save "$finalpath/dta/Portfolio_Paper_Instruments.dta", replace
* end
**************************************************************************